<template>
	<view class="goods_list">
		<goods_item :item='item' v-for="(item, i) in goodsList" :key="i" @click="gotoDetail"></goods_item>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [], //商品列表数据
				queryObj: {
					query: '', // 查询关键词
					cid: '', // 商品分类Id
					pagenum: 1, // 页码值
					pagesize: 10 // 每页显示多少条数据
				},
				total: 0, //商品列表总数
				isloading: false, //是否加载完数据

			}
		},
		methods: {
			async getGoodsList(cb) {
				this.isloading = true
				let {
					data
				} = await uni.$http.get('/goods/search', this.queryObj)
				if (data.meta.status !== 200) return uni.$showMsg()
				cb && cb()
				this.isloading = false
				this.total = data.message.total
				this.goodsList = [...this.goodsList, ...data.message.goods]
			},
			gotoDetail(data){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + data.goods_id
				})
			}
		},
		onLoad(params) {
			console.log(params);
			// 初始化列表请求查询条件
			this.queryObj.query = params.query || ''
			this.queryObj.cid = params.cid || ''
			// 调用获取商品列表数据的方法
			this.getGoodsList()
		},
		//页面触底事件
		onReachBottom() {
			if (this.goodsList.length < this.queryObj.pagesize * this.queryObj.pagenum) return uni.$showMsg('数据加载完毕！')
			if (this.isloading) return
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		//上拉刷新事件
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
	.goods_list {
		border: 1rpx solid #efefef;
		margin: 0 8rpx;
	}
</style>